/**
 * 
 *	Validate
 *
 **/
(function( $ ) {
	$.fn.validate = function( options ) {
		var defaults = {
			ajax: false,
			onSubmit: function(){},
			rules: [],
			alert: function( el ) {
				var layer = $( '<div class="alertLayer">' + el.data('message') + '</div>' );
				el.focus();
				layer
					.appendTo( document.body )
					.css({
						'top': el.offset().top + el.height(),
						'left': el.offset().left,
						'width': el.width()
					});
					
				setTimeout(function() {
					layer.remove();
				}, 2000);
			}
		};
		var o = $.extend( defaults, options );
		return this.each(function() {
			if ( ! $(this).is('form') ) {
				return;
			}
			var that = $( this );
			this.validate = true;
			for ( var i = 0, len = o.rules.length; i < len; i++ ) {
				var rule = o.rules[ i ],
					el = that.find( '[name^=' + rule.name + ']' );
				if ( rule.init ) {
					el
						.val( rule.init )
						.bind({
							'focus.validate': function() {
								el.val() === rule.init && el.val('');
							},
							'blur.validate': function() {
								$.trim( el.val() ) === '' && el.val( rule.init );
							}
						});
				}
				if ( rule.message ) {
					el.data( 'message', rule.message );
				}
			}
			
			that.submit(function(e) {
				var valid = true;
				for ( var i = 0, len = o.rules.length; i < len; i++ ) {
					var rule = o.rules[ i ],
						el = that.find( '[name=' + rule.name + ']' );
					
					if ( ! el.length ) {
						continue;
					}
					var value = el.val();
					if (rule.editor){
						if (CKEDITOR && CKEDITOR.instances){
							if (CKEDITOR.instances[rule.name]){
								value = CKEDITOR.instances[rule.name].getData();
								el.next().data( 'message', el.data('message') );
								el = el.next();
							}
						}
					}
					if ( rule.required ) {
						if ( ! value || value === rule.init ) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if( rule.tag){
						if(that.children(':hidden').last().val() == 0){
							if ( ! value || value === rule.init ) {
								o.alert( el );
								valid = false;
								return false;
							}
						}
					}
					if( rule.number){
						if ( ! /^[0-9]+$/.test(value) ) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					/* if( rule.numberFormat){
						if ( ! /^[0-9 +,]+$/.test(value) || /\s/.test(value)) {
							o.alert( el );
							valid = false;
							return false;
						}
						if(valid){
							var length = value.length;
							var text="";
							if(length <= 3 ){
								if(/,/.test(value)){
									o.alert( el );
									valid = false;
									return false;
								}
							}
							else if(length > 3){
								for (var x = 0; x < value.length; x++){
									text = value.substring(x, x+1) + text;
								}
								for(var y = 0 ; y < text.length; y++){
									if(text.search(',') == 3 ){
										text = text.substring(4);
									}
									else {
										o.alert( el );
										valid = false;
										return false;
									}
								}
							}
						}
					} */
					if(rule.numberFormat){
						if ( ! /^[0-9 +.]+$/.test(value) || /\s/.test(value)) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if ( rule.checked ) {
						if(!el.prop('checked')){
							o.alert( el );
							valid = false;
							return false;
						}
					}
					/* if(rule.logo){
						if(that.children('input').last().val() !=0){
							var wrapper = el.closest('div'),
								nextWapper = wrapper.next();
							var checkLogo = true;
							if(!value){
								o.alert( el );
								valid = false;
								return false;
							}
							else if(valid){
								checkLogo = false;
							}		
							if(!nextWapper.length && checkLogo){
								o.alert( el );
									valid = false;
									return false;
							}
						}
						else if(!value){
							o.alert( el );
							valid = false;
							return false;
						}
					} */
					if(rule.gallery){
						var wrapper = el.closest('div'),
							prevWapper = wrapper.prev();
						if(prevWapper.find('tr').length <= 1){
							o.alert( el );
								valid = false;
								return false;
						}
					}
					/* if(rule.constrainTag){
						if(rule.constrain == that.children(':hidden').first().val()){
							if ( ! value || value === rule.init ) {
								o.alert( el );
								valid = false;
								return false;
							}
						}
					} */
					if( rule.repassword){
						if (value != that.find( '[name=' + rule.checkField + ']' ).val()) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if ( rule.email ) {
						if ( ! /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/.test(value) ) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if ( rule.phone ) {
						if ( ! /^[0-9]+$/.test(value) ) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if ( rule.selected ) {
						if (value == 0 || value =='') {
							o.alert( el );
							valid = false;
							return false;
						}
					}
					if(rule.selectedCus){
						if ( /select/gi.test(value) ) {
							o.alert( el );
							valid = false;
							return false;
						}
					}
				}
				if ( ! valid ) {
					return false;
				} else {
					$.isFunction( o.onSubmit ) && o.onSubmit.apply( that, arguments );					
					if ( o.ajax ) {
						return false;
					}
				}
				return true;
			});
		});
	};
})( jQuery );
